package com.example.user.customviewdemo.drawing.geometry;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by user on 2016/4/21.
 * 练习绘制各种几何图形
 */
public class MyGeometryView extends View {
    public MyGeometryView(Context context) {
        super(context);
    }

    public MyGeometryView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGeometryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.GRAY);//设置画布颜色

        /*************画圆环**********/
        Paint paint = new Paint();
        paint.setAntiAlias(true);//设置抗锯齿功能
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.drawCircle(100, 100, 100, paint);

        /**************画圆圈******************/
        paint.setStyle(Paint.Style.FILL);//paint默认为Sytel.FILL
        canvas.drawCircle(350, 100, 100, paint);

        /**************设置阴影***********************/
        paint.setShadowLayer(10, 50, 50, Color.BLACK);//设置阴影对几何图形不起作用
        canvas.drawCircle(600, 100, 100, paint);

        /**************绘制直线*************************/
        canvas.drawLine(0, 300, 200, 300, paint);

        /****************绘制多条直线******************************/
        float[] pts = {250, 350, 450, 300, 460, 300, 660, 350, 670, 350, 870, 350};//从第一个点开始两个点连成一条直线，而不是全部链接
        canvas.drawLines(pts, paint);

        /*****************绘制点****************************/
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(50);
        canvas.drawPoint(100, 500, paint);

        /*****************绘制多个点***************************/
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.STROKE);
        float[] pt1s = {100, 700, 300, 700, 500, 700};
        canvas.drawPoints(pt1s, paint);

        /******************绘制矩形********************************/
        //直接绘制
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(10,800,210,1000,paint);
        //先构造rect，然后再绘制
        Rect rect = new Rect(220,800,420,1000);
        canvas.drawRect(rect,paint);
        //先够着rectf，然后绘制
        RectF rectF = new RectF(450,800,750,1000);
        canvas.drawRect(rectF,paint);



        /***************绘制圆角矩形***********************************/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawRoundRect(30,1100,230,1300,30,30,paint);//api大于21（系统版本大于5.0）
        }
        RectF roundRectF = new RectF(300,1100,500,1300);
        canvas.drawRoundRect(roundRectF,60,30,paint);

        /***********************绘制椭圆(椭圆是根据矩形得到的)**************************************/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawOval(600,1100,900,1300,paint);
        }
        RectF ovalRectF = new RectF(10,1300,310,1500);
        canvas.drawRect(ovalRectF,paint);
        paint.setColor(Color.GREEN);
        canvas.drawOval(ovalRectF,paint);


        /********************画弧(椭圆根据矩形而来，而弧是椭圆的一部分，所以也是根据矩形而来)******************************/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawArc(350,1300,550,1500,0,90,false,paint);//false表示只有一条弧，true表示还有两边
            canvas.drawArc(600,1300,800,1500,0,180,true,paint);
        }
        paint.setStyle(Paint.Style.FILL);
        paint.clearShadowLayer();//shadowLayer对其他的几何图形没得影响，但是当把弧的userCenter设置成true时却有影响
        RectF arcRectF = new RectF(400,400,800,600);
        canvas.drawArc(arcRectF,0,270,true,paint);


    }
}
